package com.sxhalo.fsms.controller.api;

import com.sxhalo.fsms.controller.BaseController;

import com.sxhalo.fsms.domain.DeviceRunningStateUpdateTime;
import com.sxhalo.fsms.service.IDeviceRunningStateUpdateTimeService;

import com.sxhalo.fsms.service.IFactDeviceService;
import com.sxhalo.fsms.util.ApiResult;
import com.sxhalo.fsms.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api")
public class OperationAnalysisController extends BaseController {


	@Autowired
	private IDeviceRunningStateUpdateTimeService updateStateService;
	@Autowired
	private IFactDeviceService deviceService;

	@ResponseBody
	@RequestMapping(value = "/operationAnalysis", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	public ApiResult findRole(Integer typeId,Integer areaId, HttpServletRequest req) {
		String where = " d.delete_flag=0 and d.use_state=1";
		if(typeId!=null){
			where+=" and  d.device_type_id ="+typeId;
		}

		if(areaId!=null){
			where+=" and   FIND_IN_SET(d.area_id,fun_get_area_chindids(" + areaId + "))";
		}
		String order = "";
		List<DeviceRunningStateUpdateTime> list = updateStateService.findPagerList(0, -1, where, order);
		return ApiResult.retOK(req, list);
	}



	@ResponseBody
	@RequestMapping(value = "/totalDeviceState", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	public ApiResult totalDeviceState(Integer typeId,Integer areaId, HttpServletRequest req) {
		String where = " delete_flag=0 and use_state=1";
		if(typeId!=null){
			where+=" and  device_type_id ="+typeId;
		}

		if(areaId!=null){
			where+=" and   FIND_IN_SET(area_id,fun_get_area_chindids(" + areaId + "))";
		}
		int  deviceCount=deviceService.getCount(where);
		List<HashMap> list = updateStateService.totalDeviceState(where);



		Map<String,Object> totalMap=new HashMap<>();
		totalMap.put("allCount",deviceCount);

		for(HashMap<String,Object> map:list){
			switch(Integer.parseInt(map.get("state").toString())) {
				case 1:
					totalMap.put("runCount",map.get("value"));
					map.put("name","运行中");
					break;
				case 2:
					totalMap.put("alarmCount",map.get("value"));
					map.put("name","报警");
					break;
				case 3:
					totalMap.put("faultCount",map.get("value"));
					map.put("name","故障");
					break;
				default:
					totalMap.put("noOnCount",map.get("value"));
					map.put("name","未开机");
					break;
			}
		}

		totalMap.put("chartData",list);
		return ApiResult.retOK(req, totalMap);
	}



	@ResponseBody
	@RequestMapping(value = "/totalRunStateByDevice", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	public ApiResult totalRunStateByDevice(Integer typeId,Integer areaId, HttpServletRequest req) {
		String where = " delete_flag=0 and use_state=1";
		if(typeId!=null){
			where+=" and  device_type_id ="+typeId;
		}

		if(areaId!=null){
			where+=" and   FIND_IN_SET(area_id,fun_get_area_chindids(" + areaId + "))";
		}
		Map<String,Object> resultMap=new HashMap<>();

		ArrayList<Object> seriesList=new ArrayList<>();
		ArrayList<Object> deviceList=new ArrayList<>();
		ArrayList<Object> runList=new ArrayList<>();
		ArrayList<Object> alarmList=new ArrayList<>();
		ArrayList<Object> falutList=new ArrayList<>();
		ArrayList<Object> noOnList=new ArrayList<>();
		List<HashMap> list = updateStateService.totalRunStateByDevice(where);
		for (HashMap map:list){
			deviceList.add(map.get("device_name"));
			runList.add(map.get("run_num"));
			alarmList.add(map.get("alarm_num"));
			falutList.add(map.get("fault_num"));
			noOnList.add(map.get("no_on_num"));
		}

		seriesList.add(formatItemMap("运行","#4FD97B",runList,"bar"));
		seriesList.add(formatItemMap("报警","#FFB550",alarmList,"bar"));
		seriesList.add(formatItemMap("故障","#666666",falutList,"bar"));
		seriesList.add(formatItemMap("未开机","#B0ADAD",noOnList,"bar"));
		resultMap.put("deviceList",deviceList);
		resultMap.put("seriesList",seriesList);
		return ApiResult.retOK(req, resultMap);
	}


	private Map<String,Object> formatItemMap(String name,String color,ArrayList<Object> list,String type){
		Map<String,Object> itemMap=new HashMap<>();
		itemMap.put("name",name);
		itemMap.put("type",type);
		if(type.equals("bar"))
		   itemMap.put("stack","total");
		itemMap.put("color",color);
		itemMap.put("data",list);
		return itemMap;

	}

	@ResponseBody
	@RequestMapping(value = "/totalRunStateByState", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	public ApiResult totalRunStateByState(String type,String date, Integer typeId,Integer areaId, HttpServletRequest req) {
		String where = formatDate(type,date);
		if(typeId!=null||areaId!=null){
			where+= " and  device_id in (select device_id from fact_device where delete_flag=0 and use_state=1 and "+(typeId==null?"area_id="+areaId:"device_type_id="+typeId) +")";
		}

		where+="  GROUP BY "+(type.equals("month")?"date":type.equals("year")?"MONTH(date)":"time");

		List<HashMap> list = updateStateService.totalRunStateByState(where,type,date);
		Map<String,Object> resultMap=new HashMap<>();

		ArrayList<Object> seriesList=new ArrayList<>();
		ArrayList<Object> timeList=new ArrayList<>();
		ArrayList<Object> runList=new ArrayList<>();
		ArrayList<Object> alarmList=new ArrayList<>();
		ArrayList<Object> falutList=new ArrayList<>();
		ArrayList<Object> noOnList=new ArrayList<>();
		for (HashMap map:list){
			timeList.add(map.get("product"));
			runList.add(map.get("run_num"));
			alarmList.add(map.get("alarm_num"));
			falutList.add(map.get("fault_num"));
			noOnList.add(map.get("no_on_num"));
		}


		seriesList.add(formatItemMap("运行","#4FD97B",runList,"line"));
		seriesList.add(formatItemMap("报警","#FFB550",alarmList,"line"));
		seriesList.add(formatItemMap("故障","#666666",falutList,"line"));
		seriesList.add(formatItemMap("未开机","#B0ADAD",noOnList,"line"));

		resultMap.put("timeList",timeList);
		resultMap.put("seriesList",seriesList);
		return ApiResult.retOK(req, resultMap);
	}


	private String  formatDate(String type,String date){
		if(type.equals("month")){
			return  " DATE_FORMAT(date,'%Y-%m')='"+ date.substring(0,7)+"'";
		}	if(type.equals("year")){
			return " DATE_FORMAT(date,'%Y')='"+ date.substring(0,4)+"'";
		}
		return "date ='" + date+"'" ;

	}





	@ResponseBody
	@RequestMapping(value = "/runStateByDevice", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	public ApiResult runStateByDevice(String type,String date, Integer deviceId,HttpServletRequest req) {

		String where=formatDate(type,date);
		where+=" and   device_id ="+deviceId;
        where+="  GROUP BY "+(type.equals("month")?"date":type.equals("year")?"MONTH(date)":"time");
		Map<String,Object> resultMap=new HashMap<>();
		ArrayList<Object> timeList=new ArrayList<>();

		ArrayList<Object> runList=new ArrayList<>();
		ArrayList<Object> alarmList=new ArrayList<>();
		ArrayList<Object> falutList=new ArrayList<>();
		ArrayList<Object> noOnList=new ArrayList<>();
        List<Object> chartList=new ArrayList<>();

		ArrayList<Object> seriesList=new ArrayList<>();
		List<HashMap> list = updateStateService.totalRunStateByState(where,type,date);

		for (HashMap map:list){
			timeList.add(map.get("product"));
			runList.add(map.get("run_num"));
			alarmList.add(map.get("alarm_num"));
			falutList.add(map.get("fault_num"));
			noOnList.add(map.get("no_on_num"));

		}

		seriesList.add(formatItemMap("运行","#4FD97B",runList,"bar"));
		seriesList.add(formatItemMap("报警","#FFB550",alarmList,"bar"));
		seriesList.add(formatItemMap("故障","#666666",falutList,"bar"));
		seriesList.add(formatItemMap("未开机","#B0ADAD",noOnList,"bar"));
		resultMap.put("timeList",timeList);
		resultMap.put("seriesList",seriesList);
		return ApiResult.retOK(req, resultMap);
	}





}
